package cn.swpu.blog.config;

import cn.swpu.blog.filter.JwtFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author 冰翔
 * @Date 2021/2/19 10:16
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private  JwtFilter jwtFilter;

    /**
     * 配置Spring Security
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
       http
               //禁用csrf攻击
               .csrf(AbstractHttpConfigurer::disable)
               //设置无状态连接，不创建session
               .sessionManagement(AbstractHttpConfigurer::disable)
               //禁用表单登陆
               .formLogin(AbstractHttpConfigurer::disable)
       .authorizeRequests()
       .antMatchers("/qq/oauth","/qqLogin/callback.do","/login","/#/qq/redirect","/",
               "/blog/detail/**","/blog/allBlog","/blog/saveBlog",
               //前端
               "/favicon.ico", "/css/**","/js/**",
               //swagger
               "/doc.html","/webjars/**","/img.icons/**","/swagger-resources/**","/v2/api-docs","/error").permitAll()
       .anyRequest().authenticated()
       .and()
       .addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
    }

    /**
     * 取消拦截的资源
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers();
    }
}
